import config from 'config';
import httpFetch from 'share/httpFetch';
import errorMessage from 'share/errorMessage';

export default {
  /**
   * 获取车辆列表数据
   * @param {String} url
   */
  getListData(url) {
    return new Promise((resolve, reject) => {
      const res = httpFetch.get(`${config.baseUrl}/api/car${url}`).then((res) => {
        resolve(res);
      }).catch((err) => {
        errorMessage(err.response);
        reject(err.response);
      });

    });
  },
  /**
   * 更新一条车辆信息记录
   * @param {Object} data
   */
  updateData(data) {
    return new Promise((resolve, reject) => {
      const res = httpFetch.put('/api/car', { ...data }).then((res) => {
        resolve(res);
      }).catch((err) => {
        errorMessage(err.response);
        reject(err.response);
      });
    });
  },
  /**
   * 新增一条车辆信息记录
   * @param {Object} data
   */
  addRecord(data) {
    return new Promise((resolve, reject) => {
      const res = httpFetch.post('/api/car', { ...data }).then((res) => {
        resolve(res);
      }).catch((err) => {
        errorMessage(err.response);
        reject(err.response);
      });
    });
  },
  /**
   * 获取司机列表数据
   * @param {String} url
   */
  getDriver(url) {
    return new Promise((resolve, reject) => {
      const res = httpFetch.get(`${config.baseUrl}/api/driver/local${url}`).then((res) => {
        resolve(res);
      }).catch((err) => {
        errorMessage(err.response);
        reject(err.response);
      });
    });
  },
  exportData() {
    return new Promise((resolve, reject) => {
      const res = httpFetch.get(`${config.baseUrl}/api/car/export?all=false&page=0&size=10`, {}, {}, {responseType: 'arraybuffer'}).then((res) => {
        resolve(res);
      }).catch((err) => {
        errorMessage(err.response);
        reject(err.response);
      });
    });
  },
  getRelationListData(id, params) {
    return httpFetch.get(`${config.baseUrl}/api/carRelatedPerson/${id}`, params);
  },
  addRelationListData(params) {
    return httpFetch.post(`${config.baseUrl}/api/carRelatedPerson`, params);
  },
  deleteRelationListData(id) {
    return httpFetch.delete(`${config.baseUrl}/api/carRelatedPerson?id=${id}`);
  }
};
